using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The TeacherSubjectTableDB class is responsible for interacting with the database to retrieve and store information
    /// about TeacherSubjectTable objects.
    /// </summary>
    public class TeacherSubjectTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of TeacherSubjectTable from the underlying datasource.
        /// </summary>
        /// <param name="teacherSubjectKey">The unique TeacherSubjectKey of the TeacherSubjectTable in the database.</param>
        /// <returns>An TeacherSubjectTable when the teacherSubjectKey was found in the database, or null otherwise.</returns>
        public static TeacherSubjectTable GetItem(int teacherProfileKey, int subjectKey)
        {
            TeacherSubjectTable myTeacherSubjectTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTeacherSubjectTableSelectSingleItemBySubjectKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@teacherProfileKey", teacherProfileKey);
                    myCommand.Parameters.AddWithValue("@subjectKey", subjectKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myTeacherSubjectTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myTeacherSubjectTable;
            }
        }

        /// <summary>
        /// Gets an instance of TeacherSubjectTable from the underlying datasource.
        /// </summary>
        /// <param name="teacherSubjectKey">The unique teacherSubjectKey of the TeacherSubjectTable in the database.</param>
        /// <returns>An TeacherSubjectTable when the teacherSubjectKey was found in the database, or null otherwise.</returns>
        public static TeacherSubjectTable GetItem(int teacherSubjectKey)
        {
            TeacherSubjectTable myTeacherSubjectTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTeacherSubjectTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@teacherSubjectKey", teacherSubjectKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myTeacherSubjectTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myTeacherSubjectTable;
            }
        }

        /// <summary>
        /// Returns a list with TeacherSubjectTable objects.
        /// </summary>
        /// <returns>A generics List with the TeacherSubjectTable objects.</returns>
        public static TeacherSubjectTableList GetList(int teacherProfileKey)
        {
            TeacherSubjectTableList tempList = new TeacherSubjectTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTeacherSubjectTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@teacherProfileKey", teacherProfileKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a TeacherSubjectTable in the database.
        /// </summary>
        /// <param name="myTeacherSubjectTable">The TeacherSubjectTable instance to save.</param>
        /// <returns>The new teacherSubjectKey if the TeacherSubjectTable is new in the database or the existing teacherSubjectKey when an item was updated.</returns>
        public static int Save(TeacherSubjectTable myTeacherSubjectTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTeacherSubjectTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myTeacherSubjectTable.TeacherSubjectKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@teacherSubjectKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@teacherSubjectKey", myTeacherSubjectTable.TeacherSubjectKey);
                    }
                    if (myTeacherSubjectTable.TeacherProfileKey == null)
                    {
                        myCommand.Parameters.AddWithValue("@teacherProfileKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@teacherProfileKey", myTeacherSubjectTable.TeacherProfileKey);
                    }
                    if (myTeacherSubjectTable.SubjectKey == null)
                    {
                        myCommand.Parameters.AddWithValue("@subjectKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@subjectKey", myTeacherSubjectTable.SubjectKey);
                    }
                    if (string.IsNullOrEmpty(myTeacherSubjectTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", myTeacherSubjectTable.Description);
                    }

                    //if (myTeacherSubjectTable.Status == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@status", myTeacherSubjectTable.Status);
                    //}

                    myCommand.Parameters.AddWithValue("@status", myTeacherSubjectTable.Status);

                    //if (myTeacherSubjectTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myTeacherSubjectTable.CreatedDateTime);
                    //}
                    //if (myTeacherSubjectTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myTeacherSubjectTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a TeacherSubjectTable from the database.
        /// </summary>
        /// <param name="teacherSubjectKey">The teacherSubjectKey of the TeacherSubjectTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int teacherSubjectKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTeacherSubjectTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@teacherSubjectKey", teacherSubjectKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the TeacherSubjectTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static TeacherSubjectTable FillDataRecord(IDataRecord myDataRecord)
        {
            TeacherSubjectTable myTeacherSubjectTable = new TeacherSubjectTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TeacherSubjectKey")))
            {
                myTeacherSubjectTable.TeacherSubjectKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TeacherSubjectKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TeacherProfileKey")))
            {
                myTeacherSubjectTable.TeacherProfileKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TeacherProfileKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SubjectKey")))
            {
                myTeacherSubjectTable.SubjectKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SubjectKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                myTeacherSubjectTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myTeacherSubjectTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myTeacherSubjectTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myTeacherSubjectTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myTeacherSubjectTable;
        }
    }

}
